package com.itheima.ai.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.netty.http.client.HttpClient;

import java.time.Duration;

@Data
@Configuration
@ConfigurationProperties(prefix = "zhipu.api")
public class ZhipuConfig {
    
    private String key;
    private String baseUrl;
    private String model;
    private Integer timeout;
    private Integer maxTokens;
    private Double temperature;
    
    @Bean
    public WebClient zhipuWebClient() {
        HttpClient httpClient = HttpClient.create()
                .responseTimeout(Duration.ofMillis(timeout != null ? timeout : 60000));
        
        return WebClient.builder()
                .baseUrl(baseUrl != null ? baseUrl : "https://open.bigmodel.cn/api/paas/v4")
                .clientConnector(new ReactorClientHttpConnector(httpClient))
                .defaultHeader("Authorization", "Bearer " + key)
                .defaultHeader("Content-Type", "application/json")
                .build();
    }
}

